package org.wen.videotool;


import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.FFmpegLogCallback;
import org.bytedeco.javacv.Frame;

import java.io.File;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class VideoResizer {

    public static void resizeVideo(String inputPath, String outputPath) {
        // 启用 FFmpeg 日志
        FFmpegLogCallback.set();

        // 验证文件存在性
        File inputFile = new File(inputPath);
        if (!inputFile.exists()) {
            System.err.println("错误：输入文件不存在！");
            return;
        }

        FFmpegFrameGrabber grabber = null;
        FFmpegFrameRecorder recorder = null;

        try {
            // 处理中文路径
            String encodedPath = URLEncoder.encode(inputPath, StandardCharsets.UTF_8);
            grabber = new FFmpegFrameGrabber(encodedPath);

            // 显式设置格式
            grabber.setFormat("mp4");

            grabber.start();

            // 获取视频元数据
            int width = grabber.getImageWidth();
            int height = grabber.getImageHeight();
            double frameRate = grabber.getFrameRate();

            System.out.println("视频信息:");
            System.out.println("宽度: " + width + "px");
            System.out.println("高度: " + height + "px");
            System.out.println("帧率: " + frameRate + " FPS");


            // 初始化录制器
            recorder = new FFmpegFrameRecorder(
                    outputPath,
                    width,
                    height
            );

            // 2. 配置录制器（关键参数）
            recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
            recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P); // 兼容性最好的像素格式
            recorder.setFormat("mp4");
            recorder.setVideoBitrate(0); // 0 表示无损模式
            recorder.setFrameRate(grabber.getFrameRate()); // 同步帧率

//            recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
//            recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
//            recorder.setVideoBitrate(1_500_000);
//            recorder.setFormat("mp4");
//            recorder.setFrameRate(grabber.getFrameRate());

            if (grabber.getAudioChannels() > 0) {
                recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);
                recorder.setSampleRate(grabber.getSampleRate());
                recorder.setAudioChannels(grabber.getAudioChannels());
//                recorder.setAudioBitrate(128_000);
            }

            recorder.start();

            // 录制循环
            Frame frame;
            while ((frame = grabber.grab()) != null) {
                recorder.record(frame);
            }

            System.out.println("视频处理完成！输出路径: " + outputPath);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (grabber != null) grabber.stop();
                if (recorder != null) recorder.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
//        String inputVideo = "C:\\Users\\Administrator\\Desktop\\视频\\a-086dd2cf-43de-44aa-aad2-c162067a275c.mp4";
//        String inputVideo = "111.mp4";
        String inputVideo = "1112.MOV";
        String outputVideo = "output.mp4";

        resizeVideo(inputVideo, outputVideo);
    }



}
